library(tidyverse)
library(showtext)
library(magrittr)
library(estimatr)
library(metafor)
library(broom)

covariates <-
  "+ lucid_pid_7n +
  lucid_age +
  lucid_race +
  lucid_hhin +
  political_knowledge_pre +
  political_interest_pre +
  cognitive_reflection_pre +
  need_for_cognition_pre +
  extraversion_pre +
  agreeableness_pre +
  conscientiousness_pre +
  emotional_stability_pre +
  openness_to_experience_pre"

all_panels_long <- "https://github.com/thomasjwood/cgptw22/raw/main/all_panels_long.rds" %>% 
  url %>% 
  gzcon %>% 
  readRDS

treatments_df <- "https://github.com/thomasjwood/cgptw22/raw/main/treatments_df.rds" %>% 
  url %>% 
  gzcon %>% 
  readRDS

persistence_p2_df <-
  all_panels_long %>%
  filter(!is.na(outcome_w1),
         !is.na(outcome_w2),
         treatment != "misinformation")

persistence_p3_df <-
  all_panels_long %>%
  filter(!is.na(outcome_w1),
         !is.na(outcome_w3),
         treatment != "misinformation")


filter_and_flip <- function(data) {
  data %>%
    filter(term %in% c("treatmentcontrol", "treatmentfactcheck")) %>%
    mutate(
      estimate = if_else(term == "treatmentcontrol",-1 * estimate, estimate),
      conf.low = if_else(term == "treatmentcontrol",-1 * conf.low, conf.low),
      conf.high = if_else(term == "treatmentcontrol",-1 * conf.high, conf.high),
    )
  
}



# Average Treatment Effects at Wave 1 -------------------------------------

ates_w1_all_noadj <-
  all_panels_long %>%
  group_by(fc, panel, panel_factor, rating) %>%
  do(tidy(lm_robust(formula(
    outcome_w1 ~ treatment
  ), data = .))) %>%
  filter_and_flip

ates_w1_all_adj <-
  all_panels_long %>%
  group_by(fc, panel, panel_factor, rating) %>%
  do(tidy(lm_robust(formula(
    paste0("outcome_w1 ~ treatment +", covariates)
  ), data = .))) %>%
  filter_and_flip


ates_w1_all_adj %>%
  mutate(term2 = term) %>%
  group_by(term2) %>%
  do(tidy(rma.uni(estimate, sei = std.error, data = .), conf.int = TRUE))


ates_w1_all_adj %>%
  mutate(term2 = term) %>%
  group_by(term2, rating) %>%
  do(tidy(rma.uni(estimate, sei = std.error, data = .), conf.int = TRUE)) %>%
  ggplot() +
  aes(estimate, rating) +
  geom_point() +
  geom_linerange(aes(xmin = conf.low, xmax = conf.high)) +
  facet_grid(~term2)


# Cates -----------------------------------------------------

cates_w1_all_noadj <-
  all_panels_long %>%
  filter(lucid_pid_3 != "Independent") %>%
  group_by(fc, panel, panel_factor, lucid_pid_3) %>%
  do(tidy(lm_robust(formula(
    outcome_w1 ~ treatment
  ), data = .))) %>%
  filter_and_flip


cates_w1_all_adj <-
  all_panels_long %>%
  filter(lucid_pid_3 != "Independent") %>%
  group_by(fc, panel, panel_factor, lucid_pid_3) %>%
  do(tidy(lm_robust(formula(
    paste0("outcome_w1 ~ treatment +", covariates)
  ), data = .))) %>%
  filter_and_flip

diff_in_cates_w1_all_adj <-
  all_panels_long %>%
  filter(lucid_pid_3 != "Independent") %>%
  group_by(fc, panel, topic_short_2) %>%
  do(tidy(lm_robust(formula(
    paste0("outcome_w1 ~ treatment + lucid_pid_3 + lucid_pid_3*treatment + lucid_pid_3*(", covariates, ")")
  ), data = .))) %>% 
  filter(
    term %in% 
      c("treatmentcontrol:lucid_pid_3Republican",
        "treatmentfactcheck:lucid_pid_3Republican")
  ) %>% 
  ungroup




trait_cates_w1_all_noadj <-
  all_panels_long %>%
  pivot_longer(
    cols = c(
      grp_crt,
      grp_polint,
      grp_nfc,
      grp_openness,
      grp_conscientiousness,
      grp_extraversion,
      grp_agree,
      grp_emotstab,
      grp_pk
    )
  ) %>%
  filter(!is.na(value), value != "med") %>%
  group_by(name, value,fc, panel, panel_factor) %>%
  do(tidy(lm_robust(formula(
    outcome_w1 ~ treatment
  ), data = .))) %>%
  filter_and_flip


trait_cates_w1_all_adj <-
  all_panels_long %>%
  pivot_longer(
    cols = c(
      grp_crt,
      grp_polint,
      grp_nfc,
      grp_openness,
      grp_conscientiousness,
      grp_extraversion,
      grp_agree,
      grp_emotstab,
      grp_pk
    )
  ) %>%
  filter(!is.na(value), value != "med") %>%
  group_by(name, value, fc, panel, panel_factor) %>%
  do(tidy(lm_robust(formula(
    paste0("outcome_w1 ~ treatment +", covariates)
  ), data = .))) %>%
  filter_and_flip %>%
  mutate(type2 = term)


meta_trait_cates_w1_all_adj <-
  trait_cates_w1_all_adj %>% 
  group_by(name, value, type2) %>%
  do(tidy(rma.uni(
    yi = estimate, sei = std.error, data = .
  ), conf.int = TRUE)) 




meta_trait_dic_w1_all_adj <-
  trait_cates_w1_all_adj %>% 
  group_by(name, type2) %>%
  do(tidy(rma.uni(
    yi = estimate, sei = std.error, mods = value, data = .
  ), conf.int = TRUE)) %>%
  filter(term == "mods")


# Trait persistence


trait_cates_w2_p2_adj <-
  persistence_p2_df %>%
  pivot_longer(
    cols = c(
      grp_crt,
      grp_polint,
      grp_nfc,
      grp_openness,
      grp_conscientiousness,
      grp_extraversion,
      grp_agree,
      grp_emotstab,
      grp_pk
    )
  ) %>%
  filter(!is.na(value), value != "med") %>%
  group_by(name, value, fc, panel, panel_factor) %>%
  do(tidy(lm_robust(formula(
    paste0("outcome_w2 ~ treatment +", covariates)
  ), data = .))) %>%
  filter_and_flip %>%
  mutate(type2 = term)



ggplot(trait_cates_w2_p2_adj, 
       aes(estimate, name, group = value, color = value)) +
  geom_point()

meta_trait_cates_w2_p2_adj <- 
  trait_cates_w2_p2_adj  %>% 
  group_by(name, value, type2) %>%
  do(tidy(rma.uni(
    yi = estimate, sei = std.error, data = .
  ), conf.int = TRUE)) 


# w1 to w2 persistence ----------------------------------------------------


ates_w1_p2_noadj <-
  persistence_p2_df %>%
  group_by(fc, panel, panel_factor) %>%
  do(tidy(lm_robust(formula(
    outcome_w1 ~ treatment
  ), data = .))) %>%
  filter_and_flip

ates_w2_p2_noadj <-
  persistence_p2_df %>%
  group_by(fc, panel, panel_factor) %>%
  do(tidy(lm_robust(formula(
    outcome_w2 ~ treatment
  ), data = .))) %>%
  filter_and_flip

iv_p2_noadj <-
  persistence_p2_df %>%
  group_by(fc, panel, panel_factor) %>%
  do(tidy(iv_robust(
    formula(outcome_w2 ~ outcome_w1 | treatment), data = .
  ))) %>% 
  filter(term == "outcome_w1")

ates_w1_p2_adj <-
  persistence_p2_df %>%
  group_by(fc, panel, panel_factor) %>%
  do(tidy(lm_robust(formula(
    paste0("outcome_w1 ~ treatment +", covariates)
  ), data = .))) %>%
  filter_and_flip

ates_w2_p2_adj <-
  persistence_p2_df %>%
  group_by(fc, panel, panel_factor) %>%
  do(tidy(lm_robust(formula(
    paste0("outcome_w2 ~ treatment +", covariates)
  ), data = .))) %>%
  filter_and_flip


iv_p2_adj <-
  persistence_p2_df %>%
  group_by(fc, panel, panel_factor) %>%
  do(tidy(iv_robust(formula(
    paste0(
      "outcome_w2 ~ outcome_w1 +",
      covariates,
      "|  treatment +",
      covariates
    )
  )
  , data = .))) %>% 
  filter(term == "outcome_w1")

# w1 to w3 persistence

ates_w1_p3_noadj <-
  persistence_p3_df %>%
  group_by(fc, panel, panel_factor) %>%
  do(tidy(lm_robust(formula(
    outcome_w1 ~ treatment
  ), data = .))) %>%
  filter_and_flip

ates_w2_p3_noadj <-
  persistence_p3_df %>%
  group_by(fc, panel, panel_factor) %>%
  do(tidy(lm_robust(formula(
    outcome_w2 ~ treatment
  ), data = .))) %>%
  filter_and_flip

ates_w3_p3_noadj <-
  persistence_p3_df %>%
  group_by(fc, panel, panel_factor) %>%
  do(tidy(lm_robust(formula(
    outcome_w3 ~ treatment
  ), data = .))) %>%
  filter_and_flip

iv_p3_noadj <-
  persistence_p3_df %>%
  group_by(fc, panel, panel_factor) %>%
  do(tidy(iv_robust(
    formula(outcome_w3 ~ outcome_w1 | treatment), data = .
  ))) %>% 
  filter(term == "outcome_w1")


ates_w1_p3_adj <-
  persistence_p3_df %>%
  group_by(fc, panel, panel_factor) %>%
  do(tidy(lm_robust(formula(
    paste0("outcome_w3 ~ treatment +", covariates)
  ), data = .))) %>%
  filter_and_flip

ates_w2_p3_adj <-
  persistence_p3_df %>%
  group_by(fc, panel, panel_factor) %>%
  do(tidy(lm_robust(formula(
    paste0("outcome_w3 ~ treatment +", covariates)
  ), data = .))) %>%
  filter_and_flip


ates_w3_p3_adj <-
  persistence_p3_df %>%
  group_by(fc, panel, panel_factor) %>%
  do(tidy(lm_robust(formula(
    paste0("outcome_w2 ~ treatment +", covariates)
  ), data = .))) %>%
  filter_and_flip


iv_p3_adj <-
  persistence_p3_df %>%
  group_by(fc, panel, panel_factor) %>%
  do(tidy(iv_robust(formula(
    paste0(
      "outcome_w2 ~ outcome_w1 +",
      covariates,
      "|  treatment +",
      covariates
    )
  )
  , data = .))) %>% 
  filter(term == "outcome_w1")


# removing race?
covariates <-
  "+ lucid_pid_7n +
  lucid_age +
  lucid_hhin +
  political_knowledge_pre +
  political_interest_pre +
  cognitive_reflection_pre +
  need_for_cognition_pre +
  extraversion_pre +
  agreeableness_pre +
  conscientiousness_pre +
  emotional_stability_pre +
  openness_to_experience_pre"



cates_w1_p2_adj <-
  persistence_p2_df %>%
  filter(lucid_pid_3 != "Independent") %>%
  group_by(fc, panel, panel_factor, lucid_pid_3) %>%
  do(tidy(lm_robust(formula(
    paste0("outcome_w1 ~ treatment +", covariates)
  ), data = .))) %>%
  filter_and_flip

cates_w2_p2_adj <-
  persistence_p2_df %>%
  filter(lucid_pid_3 != "Independent") %>%
  group_by(fc, panel, panel_factor, lucid_pid_3) %>%
  do(tidy(lm_robust(formula(
    paste0("outcome_w2 ~ treatment +", covariates)
  ), data = .))) %>%
  filter_and_flip


cates_w1_p3_adj <-
  persistence_p3_df %>%
  filter(lucid_pid_3 != "Independent") %>%
  group_by(fc, panel, panel_factor, lucid_pid_3) %>%
  do(tidy(lm_robust(formula(
    paste0("outcome_w1 ~ treatment +", covariates)
  ), data = .))) %>%
  filter_and_flip

cates_w2_p3_adj <-
  persistence_p3_df %>%
  filter(lucid_pid_3 != "Independent") %>%
  group_by(fc, panel, panel_factor, lucid_pid_3) %>%
  do(tidy(lm_robust(formula(
    paste0("outcome_w2 ~ treatment +", covariates)
  ), data = .))) %>%
  filter_and_flip

cates_w3_p3_adj <-
  persistence_p3_df %>%
  filter(lucid_pid_3 != "Independent") %>%
  group_by(fc, panel, panel_factor, lucid_pid_3) %>%
  do(tidy(lm_robust(formula(
    paste0("outcome_w3 ~ treatment +", covariates)
  ), data = .))) %>%
  filter_and_flip



meta_ates_w1_p2_adj <-
  ates_w1_p2_adj %>%
  left_join(treatments_df, by = c("fc", "panel")) %>%
  group_by(congeniality) %>%
  do(tidy(rma.uni(
    yi = estimate, sei = std.error, data = .
  ), conf.int = TRUE)) %>%
  mutate(wave = 1,
         issues = "All Issues",
         lucid_pid_3 = "All Subjects")

meta_ates_w2_p2_adj <-
  ates_w2_p2_adj %>%
  left_join(treatments_df, by = c("fc", "panel")) %>%
  group_by(congeniality) %>%
  do(tidy(rma.uni(
    yi = estimate, sei = std.error, data = .
  ), conf.int = TRUE)) %>%
  mutate(wave = 2,
         issues = "All Issues",
         lucid_pid_3 = "All Subjects")


meta_ates_w1_p3_adj <-
  ates_w1_p3_adj %>%
  left_join(treatments_df, by = c("fc", "panel")) %>%
  group_by(congeniality) %>%
  do(tidy(rma.uni(
    yi = estimate, sei = std.error, data = .
  ), conf.int = TRUE)) %>%
  mutate(wave = 1, issues = "Issues across three waves", lucid_pid_3 = "All Subjects")

meta_ates_w2_p3_adj <-
  ates_w2_p3_adj %>%
  left_join(treatments_df, by = c("fc", "panel")) %>%
  group_by(congeniality) %>%
  do(tidy(rma.uni(
    yi = estimate, sei = std.error, data = .
  ), conf.int = TRUE)) %>%
  mutate(wave = 2, issues = "Issues across three waves", lucid_pid_3 = "All Subjects")

meta_ates_w3_p3_adj <-
  ates_w3_p3_adj %>%
  left_join(treatments_df, by = c("fc", "panel")) %>%
  group_by(congeniality) %>%
  do(tidy(rma.uni(
    yi = estimate, sei = std.error, data = .
  ), conf.int = TRUE)) %>%
  mutate(wave = 3, issues = "Issues across three waves", lucid_pid_3 = "All Subjects")


meta_cates_w1_p2_adj <-
  cates_w1_p2_adj %>%
  left_join(treatments_df, by = c("fc", "panel")) %>%
  group_by(lucid_pid_3, congeniality) %>%
  do(tidy(rma.uni(
    yi = estimate, sei = std.error, data = .
  ), conf.int = TRUE)) %>%
  mutate(wave = 1, issues = "All Issues")

meta_cates_w2_p2_adj <-
  cates_w2_p2_adj %>%
  left_join(treatments_df, by = c("fc", "panel")) %>%
  group_by(lucid_pid_3, congeniality) %>%
  do(tidy(rma.uni(
    yi = estimate, sei = std.error, data = .
  ), conf.int = TRUE)) %>%
  mutate(wave = 2, issues = "All Issues")


meta_cates_w1_p3_adj <-
  cates_w1_p3_adj %>%
  left_join(treatments_df, by = c("fc", "panel")) %>%
  group_by(lucid_pid_3, congeniality) %>%
  do(tidy(rma.uni(
    yi = estimate, sei = std.error, data = .
  ), conf.int = TRUE)) %>%
  mutate(wave = 1, issues = "Issues across three waves")

meta_cates_w2_p3_adj <-
  cates_w2_p3_adj %>%
  left_join(treatments_df, by = c("fc", "panel")) %>%
  group_by(lucid_pid_3, congeniality) %>%
  do(tidy(rma.uni(
    yi = estimate, sei = std.error, data = .
  ), conf.int = TRUE)) %>%
  mutate(wave = 2, issues = "Issues across three waves")

meta_cates_w3_p3_adj <-
  cates_w3_p3_adj %>%
  left_join(treatments_df, by = c("fc", "panel")) %>%
  group_by(lucid_pid_3, congeniality) %>%
  do(tidy(rma.uni(
    yi = estimate, sei = std.error, data = .
  ), conf.int = TRUE)) %>%
  mutate(wave = 3, issues = "Issues across three waves")


persistence_meta <-
  bind_rows(
    meta_ates_w1_p2_adj,
    meta_ates_w2_p2_adj,
    meta_ates_w1_p3_adj,
    meta_ates_w2_p3_adj,
    meta_ates_w3_p3_adj,
    meta_cates_w1_p2_adj,
    meta_cates_w2_p2_adj,
    meta_cates_w1_p3_adj,
    meta_cates_w2_p3_adj,
    meta_cates_w3_p3_adj
  )

t0 <- persistence_meta

t0$lucid_pid_3 %<>% 
  plyr::mapvalues(
    c("Democrat", 
      "Republican"),
    c("Democratic Subjects",
      "Republican Subjects")
  ) %>% 
  factor(
    c("All Subjects",
      "Democratic Subjects",
      "Republican Subjects")
  )

t0 %>% 
  ggplot() +
  geom_hline(
    yintercept = 0,
    linetype = "dotted"
  ) +
  geom_blank(
    aes(
      wave, estimate
    )
  ) + 
  geom_line(
    aes(
      wave, estimate, 
      group = congeniality
    ),
    size = .375,
    position = position_dodge(width = .2)
  ) +   
  geom_pointrange(
    aes(
      wave, estimate, 
      ymin  = conf.low,
      ymax  = conf.high,
      fill = congeniality
    ),
    fatten = 5,
    shape = 21,
    color = "black",
    position = position_dodge(width = .2)
  ) +
  geom_text(
    aes(
      x, y, label = lab
    ),
    data = tribble(
      ~x,  ~y, ~lab,
      2,   -15, "False claim congenial\nto Democrats",
      2,  5, "False claim congenial\nto Republicans"
    ) %>%
      mutate(
        lucid_pid_3 = "All Subjects" %>% 
          factor(
            c("All Subjects",
              "Democratic Subjects",
              "Republican Subjects")
          ),
        issues = "All Issues" %>% 
          factor(
            t0$issues %>% 
              unique
          )
      ),
    fontface = "italic",
    size = 3,
    hjust = .5,
    lineheight = .75
  ) +
  scale_x_continuous(
    breaks = 1:3,
    labels = str_c(
      "Wave ", 1:3
    ),
    expand = expansion(add = .3)
  ) +
  facet_grid(
    issues ~ lucid_pid_3,
    labeller = label_wrap_gen(15)
  ) 